﻿if (!InfoSource) var InfoSource = {};
if (!InfoSource.AppointmentReminders) InfoSource.AppointmentReminders = {};
if (!InfoSource.AppointmentReminders.EditTemplates) InfoSource.AppointmentReminders.SendIndividualAppointmentReminder = {};


/*========================================================================================*/
InfoSource.AppointmentReminders.SendIndividualAppointmentReminder.Init = function () {
	$("#apptReminderIndiv_wizardContainer")
		.width(InfoSource.PageWidth * .7)
		.height(InfoSource.PageHeight * .85);

	$("#apptReminderIndiv_previewContainer")
		.width(InfoSource.PageWidth * .29)
		.height(InfoSource.PageHeight * .85);

	$("#apptReminderIndiv_resizeGrip")
		.height(InfoSource.PageHeight * .30);



	$("#apptReminderIndiv_wizardContainer").resizable(
		{
			handles: "e",
			resize: function (e, ui) {
				var pixelsPerPercent = InfoSource.PageWidth / 100;
				var percentageWidthOfEl = ui.size.width / pixelsPerPercent;
				var remainingWidth = 99 - percentageWidthOfEl; //99 instead of 100 because the original width of the preview div + wizard div is only 99% of page width. look at width sizing code above.
				var rightW = (pixelsPerPercent * remainingWidth);
				$("#apptReminderIndiv_previewContainer").width(rightW);
			},
			maxWidth: InfoSource.PageWidth * .7, //max width of left is 70% of body width (which, inversely, makes the min width of right be 30%)
			minWidth: InfoSource.PageWidth * .29

		}
	);


	$("#apptReminderIndiv_wizardStepsContainer .search-type").buttonset();

}

/*======================================================================================*/
InfoSource.AppointmentReminders.SendIndividualAppointmentReminder.InitHandlers = function () {
	$("#apptReminderIndiv_wizardStepsContainer_chooseAccount_searchBox").on("keypress", function (e) {
		if (e.which == 13) {
			var searchbox = $(this);
			var searchtype = $("#apptReminderIndiv_wizardStepsContainer .search-type INPUT:checked").val();

			InfoSource.AppointmentReminders.SendIndividualAppointmentReminder.DoSearch(searchtype, searchbox.val());
		}
	});

	/*-------------------------------------*/
	$("#apptReminderIndiv_wizardStepsContainer").on("click", "LI.list-item", function (e) {
		var curItem = $(this);
		var LIs = curItem.siblings("li");
		LIs.removeClass("ui-state-active");
		curItem.addClass("ui-state-active");

		var containerLI = curItem.closest(".wizardStep");
		if (containerLI.hasClass("chooseAccount") || containerLI.hasClass("chooseAppliance") || containerLI.hasClass("chooseTemplate")) {
			InfoSource.AppointmentReminders.SendIndividualAppointmentReminder.ShowPreview();
		}
	});

	/*-------------------------------------*/
	$("#apptReminderIndiv_wizardStepsContainer LI.chooseAccount").on("click", "LI.list-item", function (e) {
		InfoSource.AppointmentReminders.SendIndividualAppointmentReminder.SelectSearchItem($(this));
	});




}

/*======================================================================================*/
InfoSource.AppointmentReminders.SendIndividualAppointmentReminder.SelectSearchItem = function (elItem) {
	//$("#apptReminderIndiv_wizardStepsContainer .search-results .list-item").removeClass("ui-state-active");
	//elItem.addClass("ui-state-active");

	var contractID = elItem.attr("contractid");
	var applianceContainer = $("#apptReminderIndiv_wizardStepsContainer .chooseAppliance");
	var UL = applianceContainer.find("UL");
	UL.html("");
	InfoSource.ShowLoading(applianceContainer.find(".appliance-results"));

	applianceContainer.find(".contract-id").html("Appliances For Contract: " + contractID);


	$.ajax({
		type: "POST",
		url: "/infosource/appointmentreminders/GetContractAppliances",
		data: { contractID: contractID },
		success:
            function (response) {

            	if (response && response.StatusCode == 1) {


            		var results = response.AdditionalData.results;

            		if (results.length == 0) {
            			UL.append("<li>No Results Found For: " + contractID + "</li>");
            			return;
            		}

            		for (var x in results) {
            			var curContract = response.AdditionalData.results[x];

            			UL.append("<li applianceid='" + curContract.Appliance_ID + "' contractid='" + curContract.Contract_ID + "' class='list-item'>"
									+ curContract.Appliance
									+ "</li>");
            		}
            	}
            },
		error:
            function (response) {
            	alert("search failed");
            },
		complete:
            function (response) {
            	applianceContainer.find(".generic-loading").remove();
            },

		dataType: "json"
	});
}

/*======================================================================================*/
InfoSource.AppointmentReminders.SendIndividualAppointmentReminder.DoSearch = function (searchType, searchString) {
	//console.log(searchType + " | " + searchString);

	var searchContainer = $("#apptReminderIndiv_wizardStepsContainer .search-results");
	InfoSource.ShowLoading(searchContainer);

	$.ajax({
		type: "POST",
		url: "/infosource/appointmentreminders/DoAccountSearch",
		data: { searchType: searchType, searchString: searchString },
		success:
            function (response) {
            	if (response && response.StatusCode == 1) {
            		var UL = searchContainer.find("UL");
            		UL.html("");

            		var results = response.AdditionalData.results;

            		if (results.length == 0) {
            			UL.append("<li>No Results Found For: " + searchString + "</li>");
            			return;
            		}

            		for (var x in results) {
            			var curContract = response.AdditionalData.results[x];

            			UL.append("<li contractid='"
									+ curContract.Contract_ID
									+ "' class='list-item'>"
									+ curContract.First_Name
									+ " "
									+ curContract.Last_Name
									+ " - "
									+ curContract.Serv_House_No
									+ " "
									+ curContract.Serv_addr
									+ "</li>");
            		}
            	}
            },
		error:
            function (response) {
            	alert("search failed");
            },
		complete:
            function (response) {
            	searchContainer.find(".generic-loading").remove();
            },

		dataType: "json"
	});
}

/*======================================================================================*/
InfoSource.AppointmentReminders.SendIndividualAppointmentReminder.ShowPreview = function () {
	//$("LI.chooseAccount, LI.chooseAppliance, LI.chooseTemplate", $("#apptReminderIndiv_wizardStepsContainer"))

	var elAcct = $("#apptReminderIndiv_wizardStepsContainer LI.chooseAccount LI.ui-state-active");
	var elAppliance = $("#apptReminderIndiv_wizardStepsContainer LI.chooseAppliance LI.ui-state-active");
	var elTemplate = $("#apptReminderIndiv_wizardStepsContainer LI.chooseTemplate LI.ui-state-active");
	var previewContainer = $("#apptReminderIndiv_previewContainer .previewContent");
	previewContainer.html("");

	if (elAcct.length != 0 && elAppliance.length != 0 && elTemplate.length != 0) {
		InfoSource.ShowLoading(previewContainer);

		var contractID = elAcct.attr("contractid");
		var applianceID = elAppliance.attr("applianceid");
		var templateID = elTemplate.attr("templateid");

		$.ajax({
			type: "POST",
			url: "/infosource/appointmentreminders/PreviewTemplate",
			data: { contractID: contractID, applianceID: applianceID, templateID: templateID },
			success:
            function (response) {

            	if (response && response.StatusCode == 1) {
            		previewContainer.html("");

            		var templateBody = response.AdditionalData.templateBody;

            		previewContainer.html(templateBody);
            	}
            },
			error:
            function (response) {
            	alert("retrieve template failed");
            },
			complete:
            function (response) {
            	previewContainer.find(".generic-loading").remove();
            },

			dataType: "json"
		});
	}

};





/*======================================================================================*/
$(document).ready(function () {
	InfoSource.AppointmentReminders.SendIndividualAppointmentReminder.Init();
	InfoSource.AppointmentReminders.SendIndividualAppointmentReminder.InitHandlers();
});